<template>
	<view class='addList'>
		<view class="list" v-if="datas.length">
			<view class="addOne" v-for="(item,index) in datas" :key="index" @tap.stop='selAdd(item)'>
				<view class="add dFlex jBetween_aCenter">
					<view class="addInfo">
						<view class="base dFlex jStart_aCenter">
							<view class="username">{{item.trueName}}</view>
							<view class="tel">{{item.tel}}</view>
						</view>
						<view class="info">
							{{item.area+item.address}}
						</view>
					</view>
				</view>
				<view class="ps dFlex jBetween_aCenter">
					<view class="moren dFlex jStart_aCenter" @tap.stop='setMoren(item.addressId)'>
						<view class="morenIcon imgPublic">
							<image v-if="item.is_default==1" src="https://wx.gdxixiashi.com/public/images/car/weixuan@2x.png" mode="widthFix"></image>
							<image v-if="item.is_default==2" src="https://wx.gdxixiashi.com/public/images/car/yixuan@2x.png" mode="widthFix"></image>
						</view>
						<text>设为默认</text>
					</view>
					<view class="tools dFlex jEnd_aCenter">
						<view class="toolOne dFlex jStart_aCenter" @tap.stop='opAdd(item)'>
							<view class="toolIcon imgPublic">
								<image src="../static/images/edit.png" mode="widthFix"></image>
							</view>
							<text>编辑</text>
						</view>
						<view class="toolOne dFlex jStart_aCenter" @tap.stop='del(item)'>
							<view class="toolIcon imgPublic">
								<image src="../static/images/del.png" mode="widthFix"></image>
							</view>
							<text>删除</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="list empty imgPublic" v-else>
			<image src="../static/images/empty.png" mode="widthFix"></image>
		</view>
		<view class="btns">
			<view class="btn" @tap='opAdd'>
				新增上门地址
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				datas: [],
				status:'',
			};
		},
		onLoad(e) {
			this.status = e.status?e.status:'';
		},
		onShow() {
			this.initData();
		},
		onShareAppMessage() {
			return{
				title:'洗匣士~您的家居窗帘清洗助手',
				path:'/pages/home?spreadId=' + (uni.getStorageSync('userId')?uni.getStorageSync('userId'):''),
				imageUrl:getApp().globalData.imgDomain+'public/logo.png',
				desc:'手机下单 | 专员取送 | 生态环保 | 轻奢洗护',
			}
		},
		onShareTimeline() {
			return{
				title:'洗匣士~您的家居窗帘清洗助手',
				path:'/pages/home?spreadId=' + (uni.getStorageSync('userId')?uni.getStorageSync('userId'):''),
				imageUrl:getApp().globalData.imgDomain+'public/logo.png',
				desc:'手机下单 | 专员取送 | 生态环保 | 轻奢洗护',
			}
		},
		methods: {
			// 数据
			initData() {
				this.tool.getData('Product/addressList', {
					memberId:uni.getStorageSync('userId'),
				}).then(res => {
					console.log(res);
					if (res) {
						this.datas = res;
					}
				}).catch(err => {
					console.log(err);
				})
			},
			// 添加新地址
			opAdd(add) {
				uni.navigateTo({
					url: './opAdd?addressId=' + add.addressId,
				})
			},
			// 设置默认地址
			setMoren(addressId){
				this.tool.getData('Product/setaddress', {
					addressId: addressId,
					memberId:uni.getStorageSync('userId'),
				}).then(res => {
					console.log(res);
					if (res) {
						this.initData();
					}
				}).catch(err => {
					console.log(err);
				})
			},
			//删除地址
			del(add) {
				uni.showModal({
					title:'确定删除该收货地址?',
					success:(op)=>{
						if(op.confirm){
							this.tool.getData('Product/deladdress', {
								addressId: add.addressId
							}).then(res => {
								console.log(res);
								if (res) {
									this.initData();
								}
							}).catch(err => {
								console.log(err);
							})
						}
					}
				})
			},
			// 选择地址
			selAdd(add) {
				if(!this.status){
					let pages = getCurrentPages(); //获取page
					let prevPage = pages[pages.length - 2]; //上一个页面（父页面）
					prevPage.$vm.address = add;
					uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
						delta: 1
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #FCF9F9;
	}

	.btns {
		padding: 40rpx 46rpx;
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		box-sizing: border-box;

		.btn {
			width: 100%;
			background: linear-gradient(0deg, #609ff8 0%, #1664ff 100%);
			text-align: center;
			display: block;
			padding: 24rpx 0;
			font-size: 13px;
			color: white;
			border-radius: 80rpx;
		}
	}

	.addList {
		padding: 20rpx 2% 0;
		box-sizing: border-box;

		.addOne {
			width: 100%;
			background: white;
			// padding: 0 34rpx;
			margin-bottom: 20rpx;
			box-sizing: border-box;

			.add {
				padding: 30rpx;

				.rightIcon {
					width: 32rpx;
					margin-left: 32rpx;
				}

				.addInfo {
					flex: 1;

					.base {
						font-size: 15px;

						.tel {
							margin-left: 23rpx;
						}
					}

					.info {
						font-size: 13px;
						margin-top: 24rpx;
					}
				}
			}

			.ps {
				font-size: 13px;
				padding: 26rpx;
				box-sizing: border-box;
				// border-top: 1px solid #E6E6E6;
				color: #333333;

				.toolOne {
					margin-left: 54rpx;
				}

				.morenIcon,
				.toolIcon {
					width: 40rpx;
					margin-right: 15rpx;
				}
			}
		}
	}
</style>
